//
//  LYMoneyViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/4.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYMoneyViewController.h"
#import "LYMoneyViewCell.h"
#import "LYMoneyHeaderView.h"
#import "LYMyAccountListModel.h"
#import "LYCashViewController.h"
@interface LYMoneyViewController ()<VVYRefreshTableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *models;

@property (weak, nonatomic) LYMoneyHeaderView *header;

@property (nonatomic, assign) NSInteger page;

@end

@implementation LYMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self setUpNav];
    
    
    [self setUpTableView];
    
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
}


#pragma mark - 网络请求

/**
 *  请求新数据
 */
- (void)loadNewData{
    
    
    
    [LYRequestTool getMyCountWithToken:LYToken page:@"1" per_page:@"10" success:^(NSArray<LYMyAccountListModel *> *models, NSString * balance) {
        [self.tableView.mj_header endRefreshing];
         [self.tableView.mj_footer resetNoMoreData];
        self.header.moneyValue = balance;
        self.models = [models mutableCopy];
        [self.tableView reloadData];
        self.page = 1;
        
        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_header endRefreshing];
    }];
}
/**
 *  加载更多数据
 */
- (void)loadMoreData{
    
    NSString *page = [NSString stringWithFormat:@"%zd",self.page + 1];
    NSString *perpage = @"10";
    
    [LYRequestTool getMyCountWithToken:LYToken page:page per_page:perpage success:^(NSArray<LYMyAccountListModel *> *models, NSString * balance) {
        
        if (models.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.models addObjectsFromArray:models];;
        self.page++;
        [self.tableView reloadData];

        
        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_footer endRefreshing];
    }];

    
    
}

/**
 *  提现申请
 */
- (void)applyChangeCash{


    WS(weakSelf)
    [LYRequestTool applyChangeCashWithToken:LYToken money:self.header.moneyValue success:^{
        
        [MBProgressHUD showSuccess:@"申请提现成功"];

        
        
    } failure:^(NSString *code, NSString *message) {
        
        [MBProgressHUD showOnlyText:message];
        
    }];


}

#pragma mark - 上下拉刷新
/**
 *  下拉
 */
- (void)tableViewHeaderRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadNewData];
    
}

/**
 *  上拉
 */
- (void)tableViewfooterRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadMoreData];
    
}

#pragma mark - 初始化

- (void)setUpTableView{
    
    LYMoneyHeaderView *header = [LYMoneyHeaderView viewForNib];
    WS(weakSelf)
    header.block = ^(CGFloat money){
        
        LYCashViewController * cashVC = [[LYCashViewController alloc] initWithMoney:money];
        [weakSelf.navigationController pushViewController:cashVC animated:YES];
    
    };
    header.frame = CGRectMake(0, 0, LYScreenWidth, 92);
    self.tableView.tableHeaderView = header;
    self.header = header;
    
}

- (void)setUpNav{

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"提现" style:UIBarButtonItemStyleDone target:self action:@selector(applyChangeCash)];
    self.navigationItem.rightBarButtonItem = rightItem;

}


#pragma mark - tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
    
}

#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"LYMoneyViewCell";
    LYMoneyViewCell *cell = [LYMoneyViewCell cellWithTableView:tableView andIndetifier:cellID];
    
    //model
    LYMyAccountListModel *model = self.models[indexPath.row];
    cell.model = model;
    
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


@end
